//
// Created by 67678 on 2023/3/12.
//
#include "threadPool.h"
#include "log.h"

using namespace threadPool;


void ThreadPool::init(int capacity) {
    log("threadPool initiating!");

    capacity_ = capacity;
    for (int i = 0; i < capacity_; ++i) {
        thread t(Worker{});
        t.detach();
    }
}

future<void> ThreadPool::commit(JOB_TYPE job) {
    auto fut = job->get_future();
    Worker::commit(std::move(job));
    return fut;
}

ThreadPool::ThreadPool() {
    log("threadPool start running!");

}

ThreadPool::~ThreadPool() {
    log("threadPool end running!");

    close();
}

void ThreadPool::close() {
    if (done_ == true) return;

    log("threadPool closing!");

    done_ = true;
    Worker::finish();
}
